const webpack = require('webpack');
const path = require('path');

const setPort = (env) => {
	console.log(
		{
			index: 8080,
			base: 8081
		}[env.REACT_APP_ENTRY]
	);
	return {
		index: 8080,
		base: 8081
	}[env.REACT_APP_ENTRY];
};

module.exports = (env, argv) => {
	return {
		mode: 'development',
		devtool: 'source-map',
		devServer: {
			hot: true,
			static: {
				directory: path.join(__dirname, 'public')
			},
			// 启用了  historyApiFallback  选项来支持前端路由
			historyApiFallback: true,
			host: '127.0.0.1',
			port: setPort(env),
			compress: true,
			open: true,
			proxy: {
				[env.REACT_APP_BASE_API]: {
					target: `https://mock.apifox.cn`,
					changeOrigin: true,
					// 跨域配置
					pathRewrite: {
						['^' + env.REACT_APP_BASE_API]: '/m1/544622-0-default'
					}
				}
			}
		},
		plugins: [new webpack.HotModuleReplacementPlugin()],
		externals: {
			// 模块名: 全局变量
			// react: 'React',
			// 'react-dom': 'ReactDOM'
		}
	};
};
